<?php
class Programa extends ProgramasAppModel {

	var $name = 'Programa';
	var $validate = array(
		'title' => array('notempty'),
		'dias' => array('notempty')
	);

	var $hasOne =array(
		'Image' => array(
			'className' => 'Images.Image',
			'foreignKey' => 'owner_id',
			'conditions' => 'Image.model = "Programa"',
			'fields' => '',
			'order' => ''
		)
	);
	
	
	function get_programa(){
		$time = date("H:i:s");
		$dia = date("N");
		$conditions = " dias like '%".$dia."%'  AND horario_inicio <= '".$time."' AND horario_fin >= '".$time."'";
		$programa = $this->find('first',array('conditions'=> $conditions));
		if(!empty($programa)){
			return $programa;
		}
		$conditions = " dias like '%".$dia."%' AND  horario_inicio > horario_fin AND ((horario_inicio <= '".$time."' AND horario_fin <= '".$time."') OR (horario_inicio >= '".$time."' AND horario_fin >= '".$time."'))";
		$programa = $this->find('first',array('conditions'=> $conditions));
		return $programa;	
	}
}
?>